package com.wy.statusbar;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private String TAG = "MainActivity";

    private View mTitleBar;

    private MyScollView scrollView;

    private ImageView mImageView;

    private int imageHeight;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        StatusBarUtil.setStatusBarColor(this, Color.BLUE);
//        StatusBarUtil.setActivityTranslucent(this);

        mTitleBar = findViewById(R.id.title_bar);
        scrollView = findViewById(R.id.scrollView);
        mImageView = findViewById(R.id.image_view);
        mTitleBar.setAlpha(0);

        mImageView.post(new Runnable() {
            @Override
            public void run() {
                imageHeight = mImageView.getMeasuredHeight();
            }
        });

        //QQ效果 1.不断监听scollview的滚动，判断当前滚动的位置跟头部的imageview比较计算背景透明度
        //2.自定义behavior

        scrollView.setScrollChangerListener(new MyScollView.ScrollChangerListener() {
            @Override
            public void onScroll(int l, int t, int oldl, int oldt) {

                if (0 == t || 0 == imageHeight) {
                    return;
                }
                //获取图片的高度，根据当前滚动的高度，来改变透明度
                float alpha = (float) t / imageHeight;
                mTitleBar.setAlpha(alpha);
            }
        });

    }

}